package com.ruoyi.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.StarStudio.Entity.*;
import com.ruoyi.StarStudio.Service.OrderService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 条件查询 订单分页接口
     * @param orders 订单对象
     * @return 分页结果对象 PageResult
     */
    @GetMapping("/page")
    public PageResult page(Orders orders){
        PageUtils.startPage();
        List<Orders> ordersList;
        long allRecordNumber;
        try {
            ordersList = orderService.findOrdersList(orders);
            allRecordNumber = orderService.findAllRecordNumber();
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.QUERY_PAGE_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.QUERY_PAGE_SUCCESS, new PageInfo<>(ordersList).getTotal(), ordersList, allRecordNumber);
    }

    /**
     * 新增订单
     * @param orders 订单对象
     * @return 结果对象 Result
     */
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Result NewOrder(@RequestBody Orders orders){
        try {
            orderService.NewOrder(orders);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage().equals("商品库存不足！")){
                return new Result(HttpStatusCode.WARNING,MessageConstant.OUT_OF_GOODS_STOCK);
            } else if (e.getMessage().equals("优惠券库存不足！")) {
                return new Result(HttpStatusCode.WARNING,MessageConstant.OUT_OF_COUPONS_STOCK);
            } else if (e.getMessage().equals("该订单已不可取消！")) {
                return new Result(HttpStatusCode.WARNING,MessageConstant.CAN_NOT_CANCEL_ORDER);
            }else {
                return new Result(HttpStatusCode.ERROR,MessageConstant.SERVER_ERROR);
            }
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.ADD_ORDER_SUCCESS);
    }

    /**
     * 修改订单
     * @param orders 订单对象
     * @return 结果对象 Result
     */
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result UpdateOrder(@RequestBody Orders orders){
        try {
            orderService.UpdateOrder(orders);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.UPDATE_ORDER_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.UPDATE_ORDER_SUCCESS);
    }

    /**
     * 根据订单ID取消订单
     * @param order_id 订单ID
     * @return 结果对象 Result
     */
    @Log(title = "订单管理", businessType = BusinessType.CANCEL)
    @DeleteMapping("/{order_id}")
    public Result CancelOrder(@PathVariable long order_id){
        try {
            orderService.CancelOrder(order_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.CANCEL_ORDER_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.CANCEL_ORDER_SUCCESS);
    }

    /**
     * 导出订单信息到 Excel表格
     * @param response http响应
     * @param orders 订单对象
     */
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders) {
        List<Orders> list = orderService.findOrdersList(orders);
        ExcelUtil<Orders> util = new ExcelUtil<>(Orders.class);
        util.exportExcel(response, list, "订单数据");
    }
}
